package study.lgf.action;

import study.lgf.model.Message;
import study.lgf.model.People;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class MessageAction extends ActionSupport {
	private Message message;
	private String type;
	private String userName;
	private People personBean;

	@Override
	public String execute() throws Exception {
		if (message == null) {
			message = new Message();
		}

		if (userName != null) {
			message.setMessage(userName + message.getMessage());
		}
		return SUCCESS;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public People getPersonBean() {
		return personBean;
	}

	public void setPersonBean(People personBean) {
		this.personBean = personBean;
	}

	
	@Override
	public void validate() {
		if(personBean != null){
			if (personBean.getFirstName().length() == 0) {
				addFieldError("personBean.firstName", "First name is required.");
			}
			if (personBean.getEmail().length() == 0) {
				addFieldError("personBean.email", "Email is required.");
			}
			if (personBean.getAge() < 18) {
				addFieldError("personBean.age",
						"Age is required and must be 18 or older");
			}
		}
	}
}
